<template>
  <div class="system-user-dialog-container">
    <el-dialog v-model="state.dialog.isShowDialog" :title="state.dialog.title" width="769px">
      <el-form ref="userDialogFormRef" :model="state.ruleForm" label-width="100px" size="default">
        <el-row>
          <el-col :span="12">
            <el-form-item label="操作模块：">{{ state.ruleForm.title }} / {{
                state.ruleForm.businessTypeName
              }}
            </el-form-item>
            <el-form-item
                label="登录信息："
            >{{ state.ruleForm.operName }} / {{ state.ruleForm.operIp }} / {{ state.ruleForm.operLocation }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求地址：">{{ state.ruleForm.operUrl }}</el-form-item>
            <el-form-item label="请求方式：">{{ state.ruleForm.requestMethod }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="操作方法：">{{ state.ruleForm.method }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="请求参数：">{{ state.ruleForm.operParam }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="返回参数：">{{ state.ruleForm.jsonResult }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="操作状态：">
              <div v-if="state.ruleForm.status === 0">正常</div>
              <div v-else-if="state.ruleForm.status === 1">失败</div>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="消耗时间：">{{ state.ruleForm.costTime }}毫秒</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="操作时间：">{{ state.ruleForm.operTime }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item v-if="state.ruleForm.status === 1" label="异常信息：">{{
                state.ruleForm.errorMsg
              }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
				<span class="dialog-footer">
					<el-button size="default" @click="onCancel">取 消</el-button>
				</span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" name="systemUserDialog" setup>
import {nextTick, onMounted, reactive, ref} from 'vue';
import type {FormInstance} from 'element-plus';
import {ElMessage} from 'element-plus';
import {serverCategoryApi} from "/@/api/store/server/category/server";
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const userDialogFormRef = ref();
const state = reactive({
  ruleForm: {
    title: '', // 操作模块
    businessType: '', //业务类型
    businessTypes: '',
    businessTypeName: '',
    method: '',
    requestMethod: '',
    operatorType: '',
    operName: '',
    operUrl: '',
    operIp: '',
    operLocation: '',
    operParam: '',
    jsonResult: '',
    status: '',
    errorMsg: '',
    operTime: '',
    costTime: '',
  },
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: '',
  }
});
// 打开弹窗
const openDialog = (type: string, row: RowUserType) => {
  state.dialog.type = type;
  state.dialog.isShowDialog = true;
  state.dialog.title = '日志详情';
  nextTick(() => {
    if (type === 'add') {
      // 清空表单，此项需加表单验证才能使用
      userDialogFormRef.value.resetFields();
    } else {
      state.ruleForm = Object.assign(state.ruleForm, row);
    }
  });
};
// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
  closeDialog();
};
// 提交
const onSubmit = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      if (state.dialog.type === 'add') {
        serverCategoryApi()
            .addRecord(state.ruleForm)
            .then((res) => {
              if (res && res.code === 200) {
                ElMessage.success(res.msg || '新增成功');
                closeDialog();
                emit('refresh');
              }
            });
      } else if (state.dialog.type === 'edit') {
        serverCategoryApi()
            .updateRecord(state.ruleForm)
            .then((res) => {
              if (res && res.code === 200) {
                ElMessage.success(res.msg || '修改成功');
                closeDialog();
                emit('refresh');
              }
            });
      }
    }
  });
};

// 页面加载时
onMounted(() => {
});
// 暴露变量
defineExpose({
  openDialog,
});
</script>
